package com.dmw.controller;

import com.dmw.dto.Dto;
import com.dmw.entity.DmItemComment;
import com.dmw.service.ItemService;
import com.dmw.util.DtoUtil;
import com.dmw.util.EmptyUtils;
import com.dmw.vo.FamilyPageVO.ItemInfoVO;
import com.dmw.vo.FamilyPageVO.QueryHotCityVO;
import com.dmw.vo.ImgVO.PageImgVO;
import com.dmw.vo.homePageVO.*;
import com.dmw.vo.itemInfoVO.*;
import com.dmw.vo.itemsListVO.SearchTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Configuration
public class ItemProviderController {

    @Autowired
    private ItemService itemService;

    /**
     * 商品详情页API 6-1 根据商品id返回商品详情页
     * @param map
     * @return
     */
    @PostMapping("/queryItemDetail")
    public Dto queryItemDetail(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");

        Long id = null;
        if(!EmptyUtils.isEmpty(map.get("id"))){
            id =  Long.valueOf(map.get("id").toString());
        }
        List<ItemInfoOfItemIdVO> list = null;
        try {
            list = itemService.queryItemDetail(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list.get(0));
    }

    /**
     * 商品详情页API 6-2 根据商品ID查询商品排期
     * @param map
     * @return
     */
    @PostMapping("/queryItemScheduler")
    public Dto queryItemScheduler(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");

        Long itemId = null;
        if(!EmptyUtils.isEmpty(map.get("itemId"))){
            itemId =  Long.valueOf(map.get("itemId").toString());
        }
        List<ItemsOfItemIdVO> list = null;
        try {
            list = itemService.queryItemScheduler(itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 商品详情页API 6-3 根据商品排期查询商品价格
     * @param map
     * @return
     */
    @PostMapping("/queryItemPrice")
    public Dto queryItemPrice(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");

        Long scheduleId = null;
        if(!EmptyUtils.isEmpty(map.get("scheduleId"))){
            scheduleId =  Long.valueOf(map.get("scheduleId").toString());
        }
        List<ItemsPriceOfScheduleIdVO> list = null;
        try {
            list = itemService.queryItemPrice(scheduleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 商品详情页API 6-4 根据商品id查询剧评
     * @param map
     * @return
     */
    @PostMapping("/queryItemComment")
    public Dto queryItemComment(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");
//        System.out.println(map.get("id"));

        Long itemId = null;
        if(!EmptyUtils.isEmpty(map.get("id"))){
            itemId =  Long.valueOf(map.get("id").toString());
        }
        List<ScoreOfItemIdVO> list = null;
        try {
            list = itemService.queryItemComment(itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 商品详情页API 6-5 添加剧评
     * @param vo
     * @return
     */
    @PostMapping("/commitItemComment")
    public Dto commitItemComment(@RequestBody AddScoreVO vo){
        System.out.println(">>>come");
        DmItemComment comment=new DmItemComment();
        comment.setItemid(new Long(vo.getItemId()));
        comment.setUserid(new Long(vo.getUserId()));
        comment.setScore(vo.getScore());
        comment.setContent(vo.getComment());
        comment.setCreatedtime(new Date());
        try {
            itemService.commitItemComment(comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess();
    }

    /**
     * 商品详情页API 6-6 推荐接口
     * @param map
     * @return
     */
    @PostMapping("/queryItemRecommend")
    public Dto queryItemRecommend(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");
        System.out.println(map.get("itemTypeId"));
        System.out.println(map.get("limit"));

        map.put("itemTypeId",Long.valueOf(String.valueOf(map.get("itemTypeId"))));
        map.put("limit",Integer.valueOf(String.valueOf(map.get("limit"))));
        List<ItemInfoVO> list = null;
        try {
           list = itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }


    /**
     * 商品列表页API 4-2 查询分类接口
     * @param map
     * @return
     */
    @PostMapping("/queryItemType")
    public Dto queryItemType(@RequestBody Map<String,Object> map){
        System.out.println(">>>come");
//        System.out.println(map.get("parent"));
        Long parent = null;
        if(!EmptyUtils.isEmpty(map.get("parent"))){
            parent =  Long.valueOf(map.get("parent").toString());
        }
        if(parent==null){
            parent=0L;
        }
//        System.out.println(parent);
        List<SearchTypeVO> list = null;
        try {
            list = itemService.queryItemType(parent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-1 根据分类查询轮播图
     * @param map
     * @return
     */
    @PostMapping("/querySlideShowPic")
    public Dto querySlideShowPic(@RequestBody Map<String,Object> map){
        System.out.println(">>>come111");
        System.out.println(map);
        List<PageImgVO> list = null;
        System.out.println(map.get("itemTypeId"));
        try {
            list = itemService.querySlideShowPic(Long.valueOf(String.valueOf(map.get("itemTypeId"))));
            System.out.println(list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-2 猜你喜欢接口
     * @param map
     * @return
     */
    @PostMapping("/queryItemLike")
    public Dto queryItemLike(@RequestBody  Map<String, Object> map){
        System.out.println(">>>come");
        List<ItemInfoVO> list =null;
        try {
            list =itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-3 精彩聚焦接口
     * @param map
     * @return
     */
    @PostMapping("/queryItemNice")
    public Dto queryItemNice(@RequestBody  Map<String, Object> map){
        System.out.println(">>>come");
        List<ItemInfoVO> list =null;
        try {
            list = itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-4 根据年龄段查询节目
     * @param map
     * @return
     */
    @PostMapping("/queryItemByAge")
    public Dto queryItemByAge(@RequestBody  Map<String, Object> map){
        System.out.println(">>>come");
        List<ItemInfoVO> list =null;
        try {
            list = itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-5 根据热门城市查询热门排行
     * @param
     * @return
     */
    @PostMapping("/queryHotCity")
    public Dto queryHotCity(){
        System.out.println(">>>come");
        List<QueryHotCityVO> list =null;
        try {
            list = itemService.queryHotCity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-6 根据热门城市查询热门排行
     * @param map
     * @return
     */
    @PostMapping("/queryItemHot")
    public Dto queryItemHot(@RequestBody  Map<String, Object> map){
        System.out.println(">>>come");
        List<ItemInfoVO> list =null;
        System.out.println(map.get("itemTypeId"));
        try {
            list = itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 亲子首页API接口 8-8 广告接口(图片为背景透明)
     * @param map
     * @return
     */
    @PostMapping("/queryAdvertising")
    public Dto queryAdvertising(@RequestBody  Map<String, Object> map){
        System.out.println(">>>come");
        List<ItemInfoVO> list =null;
        map.put("itemTypeId",map.get("itemTypeId"));
        map.put("limit",map.get("limit"));
        try {
            list = itemService.queryItemInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-1 获取所有的商品分类
     * @return
     */
    @PostMapping("/queryAllType")
    public Dto queryAllType(){
        List<ItemTypeVO> list = null;
        try {
            list = itemService.queryAllType();
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-2 获取首页横向导航数据
     * @return
     */
    @PostMapping("/queryTransverse")
    public Dto queryTransverse(){
        List<TransverseNavigationVO> list=null;
        try {
           list = itemService.queryTransverse();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-3 首页轮播图接口
     * @return
     */
    @PostMapping("/queryBanner")
    public Dto queryBanner(){
        List<PageImgVO> list = null;
        try {
            list = itemService.queryBanner();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-4 今日推荐接口
     * @return
     */
    @PostMapping("/queryTodayRecommend")
    public Dto queryTodayRecommend(){
        List<PageImgVO> list = null;
        try {
            list = itemService.queryTodayRecommend();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-5 即将开售接口
     * @return
     */
    @PostMapping("/queryToSaleItem")
    public Dto queryToSaleItem(){
        List<PageImgVO> list = null;
        try {
            list = itemService.queryToSaleItem();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-6 首页返回不同楼层展示数据
     * @return
     */
    @PostMapping("/queryFloorItems")
    public Dto queryFloorItems(){
        List<FloorShowVO> list = null;
        try {
            list = itemService.queryFloorItems();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-7 不同分类排行
     * @return
     */
    @PostMapping("/queryHotItems")
    public Dto queryHotItems(@RequestBody Map<String, Object> map){
        List<DefaultTypeVO> list = null;
        System.out.println(map.get("itemTypeId"));
        try {
            list = itemService.queryHotItems(new Long(map.get("itemTypeId").toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

    /**
     * 首页API 8-8 根据token返回用户信息
     * 未完成
     * @return
     */
    @PostMapping("/loadCurrentUserByToken")
    public Dto loadCurrentUserByToken(@RequestParam String token){
        System.out.println("come");
        Long id=Long.valueOf(token.split("-")[2]);
        List<UserInfoOfTokenVO> list =null;
        try {
            list = itemService.loadCurrentUserByToken(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(list);
    }

}
